﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using C.Unite;
using C.Entities;
using ChanExt;
using MySoft.Data;
using System.Text;
using LitJson;

namespace ArtLand.OA.Project.HR
{
    public partial class HR_VCATONList : CommonPageBase
    {

        /// <summary>
        /// 取得权限
        /// </summary>
        /// <returns></returns>
        public bool GetQX()
        {

            bool n = true;
            string UserKeys = UserPower.GetHaveKey();
            if (!UserKeys.Contains("View"))
            {
                PageContext.RegisterStartupScript("alert('您未有查看权限！');RemTab('dynamic_added_tab_tab_" + UserPower.GetKeyWord() + "');");
                n = false;
            }
            else
            {
                //if (CookInfo.EAdminTrueName().Trim() == "陈爱国")
                //{
                //    Button1.Hidden = false;
                //}
                if (!UserKeys.Contains("Add"))
                {
                    btnNew.Hidden = true;
                }
                hid_Power.Text = UserKeys;
            }
            return n;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (GetQX())
                {
                    btnNew.OnClientClick = Window1.GetShowReference("/HR/HR_VCATONChange.aspx", "请假申请");
                    BindGrid(GetWhere());
                    ShowNoData(Grid1);
                }
            }
        }

        #region BindGrid
        private void BindGrid(WhereClip wc)
        {
            Grid1.SelectedRowIndex = -1;
            // 1.设置总项数
            Grid1.RecordCount = TbsDbSession.Default.From<HR_VCATON>().Where(wc).Count();

            List<HR_VCATON> items = TbsDbSession.Default.From<HR_VCATON>().Where(wc).OrderBy(HR_VCATON._.ID.Desc).Page(Grid1.PageSize, (Grid1.PageIndex + 1)).ToList();

            // 3.绑定到Grid
            Grid1.DataSource = items;
            Grid1.DataBind();
        }



        protected void Grid1_PageIndexChange(object sender, ChanExt.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid(GetWhere());
        }


        #endregion

        #region Events

        public WhereClip GetWhere()
        {
            WhereClip wc = WhereClip.All;
           // wc = wc && (HR_VCATON._.AddID == CookInfo.EAdminid() || HR_VCATON._.AuditUserNo.Contain("," + CookInfo.EEmpID() + ","));
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                wc = wc && (HR_VCATON._.EMPLID.Contain(ttbxMyBox2.Text) || HR_VCATON._.EMNAME.Contain(ttbxMyBox2.Text));
            }
            return wc;
        }



        // 点击 TwinTriggerBox 的搜索按钮
        protected void ttbxMyBox2_Trigger2Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(ttbxMyBox2.Text))
            {
                // 执行搜索动作
                BindGrid(GetWhere());
                ttbxMyBox2.ShowTrigger1 = true;
            }
            else
            {
                Alert.ShowInTop("请输入你要搜索的关键词！");
            }
        }

        // 点击 TwinTriggerBox 的取消按钮
        protected void ttbxMyBox2_Trigger1Click(object sender, EventArgs e)
        {
            // 执行清空动作

            ttbxMyBox2.Text = "";
            ttbxMyBox2.ShowTrigger1 = false;
            BindGrid(GetWhere());
        }



        #endregion
        protected void Grid1_RowDataBound(object sender, ChanExt.GridRowEventArgs e)
        {
            string userkeys = hid_Power.Text;

            string s = e.Values[6].ToString();
            string end = e.Values[7].ToString();
            string cz = "";
            e.Values[5] = "<span ext:qtip=\"<b>开始时间：</b>" + s + "--<b>结束时间：</b>" + end + "<br><b>请假原因：</b>" + e.Values[5] + "\">" + e.Values[5] + "</span>";
            if (e.Values[9].ToString() == "0")
            {
                e.Values[9] = "<span style=\"font-weight:bold;\">未审批</span>";
                if (userkeys.Contains("Edit"))
                {
                    cz += e.Values[11].ToString() + " | ";
                }
                if (userkeys.Contains("Del"))
                {
                    cz += e.Values[12].ToString() + " | ";
                }
                if (userkeys.Contains("Audit"))
                {
                    cz += e.Values[13].ToString() + " | ";
                }
            }
            if (e.Values[9].ToString() == "1")
            {
                e.Values[9] = "<span style=\"color:red;font-weight:bold;\">已审批</span>";
                cz = e.Values[9].ToString() + " | ";
            }
            if (e.Values[9].ToString() == "2")
            {
                e.Values[9] = "<span style=\"color:#aaa;\">审批未通过</span>";
                cz = e.Values[9].ToString() + " | ";
            }
            if (cz != "")
                cz = cz.Remove(cz.Length - 3, 3);
            e.Values[10] = cz;
        }

        protected void Grid1_RowCommand(object sender, ChanExt.GridCommandEventArgs e)
        {
            int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
            if (e.CommandName == "Del")
            {
                TbsDbSession.Default.Delete<HR_VCATON>(HR_VCATON._.ID == id);
                Alert.ShowInTop("删除成功！");
                BindGrid(GetWhere());
            }

        }


    }
}